package com.hitechbangla.fctu.base.aircraft.command;

import com.hitechbangla.fctu.base.Location;
import com.hitechbangla.fctu.base.aircraft.*;
import com.hitechbangla.fctu.utility.*;
import com.hitechbangla.fctu.utility.enumall.*;
import java.io.Serializable;
import java.math.*;

/**
 *
 * @author deb kanti
 */
public class AircraftTurnCommand implements Serializable 
{
    private Aircraft aircraft;
    private float angleIncrement; // in radian
    private float turnRadius; //  radius of the turn circle for this command, this depends on the aircraft speed
    private Location turnCenter; // center of the turn circle for this command, this depends on the aircraft speed
    private float arcLength;
    
    
    public AircraftTurnCommand(Aircraft aircraft)
    {
        this.aircraft = aircraft;        
    }
    
    public void executeCommand()
    {
        determineTurnCircleProperties();
    }
    
    private void determineTurnCircleProperties()
    {           
        //angleIncrement = aircraft.getAircraftCommandState().getTurnDirection().getAngleCoefficient() *  getAngleIncrementOnAircraftTurnDegree(aircraft.getAircraftCommandState().getTurnDegree()); 
	float bankAngle = aircraft.getAircraftCommandState().getTurnDegree().getTurnVerticalAngle();
	float tanTheta = (float)Math.tan(GeneralUtil.degreeToRadian(bankAngle));
	
	//float speedPerSec = aircraft.getAircraftState().getSpeed()/3600;
	float speedkph = (float)(aircraft.getAircraftState().getSpeed() * 1.852);
	float speedmps = speedkph*5/18;	
	
	float turnRadiusMeter = (GeneralUtil.square(speedmps))/(4*tanTheta);
	this.turnRadius = turnRadiusMeter/1852;	
	
	// we are right upto this according to BAF data
	
	// second method of computation, it obviously failed
	
	/*
	float horizontalAngle = aircraft.getAircraftCommandState().getTurnDegree().getTurnHorizontalAngle();	
	float cosFunction = (float)Math.cos(GeneralUtil.degreeToRadian(horizontalAngle));	
	this.arcLength = (float)Math.sqrt(2 * turnRadius * turnRadius * (1-cosFunction));			
	*/
	
	float omegaInRadian = aircraft.getAircraftState().getSpeed()/(3600*this.turnRadius);
	float alphaInRadian = omegaInRadian/2;
	float cosFunction = (float)Math.cos(omegaInRadian);	
	this.arcLength = (float)Math.sqrt(2 * turnRadius * turnRadius * (1-cosFunction));

	float angleCoefficient = aircraft.getAircraftCommandState().getTurnDirection().getAngleCoefficient();        
	angleIncrement =  GeneralUtil.radianToDegree(alphaInRadian) * angleCoefficient;	
	//angleIncrement = aircraft.getAircraftCommandState().getTurnDegree().getTurnHorizontalAngle()*angleCoefficient;
	// this angleInIncrement is in degree

    }
    
    private float getAngleIncrementOnAircraftTurnDegree(TurnDegree turnDegree)
    {        
        if(turnDegree == TurnDegree.NORMAL)
        {
            return aircraft.getAircraftType().getAircraftTurnDegree().getNormal();
        }
        else if(turnDegree == TurnDegree.SLACK) 
        {
            return aircraft.getAircraftType().getAircraftTurnDegree().getSlack();            
        }
        else
        {
            return aircraft.getAircraftType().getAircraftTurnDegree().getTight();
        }
    }

    /**
     * @return the angleIncrement
     */
    public float getAngleIncrement() 
    {
        return angleIncrement;
    }

    /**
     * @return the turnCenter
     */
    public Location getTurnCenter() {
        return turnCenter;
    }

    /**
     * @return the turnRadius
     */
    
    public float getTurnRadius() {
        return turnRadius;
    }

    /**
     * @return the arcLength
     */
    public float getArcLength() {
	return arcLength;
    }
}
